<?php
namespace plugin\ky_shouyin\app\controller;

use support\Request;
use support\Db;

class MemberLevelController extends BaseController
{
    public function index()
    {
        return $this->view('member/level/index');
    }
    
    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 15);
        
        $query = Db::table('ky_member_level');
        
        $total = $query->count();
        $levels = $query->offset(($page - 1) * $limit)
                       ->limit($limit)
                       ->orderBy('id', 'asc')
                       ->get();
                       
        return json([
            'code' => 0,
            'msg' => 'success',
            'count' => $total,
            'data' => $levels
        ]);
    }
    
    public function add(Request $request)
    {
        if ($request->method() === 'POST') {
            $data = $request->post();
            
            try {
                // 验证等级名称是否重复
                $exists = Db::table('ky_member_level')
                    ->where('name', $data['name'])
                    ->exists();
                    
                if ($exists) {
                    throw new \Exception('等级名称已存在');
                }
                
                // 添加等级
                Db::table('ky_member_level')->insert([
                    'name' => $data['name'],
                    'discount' => $data['discount'],
                    'points_rate' => $data['points_rate'],
                    'upgrade_points' => $data['upgrade_points'],
                    'status' => $data['status'],
                    'remark' => $data['remark'],
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
                
                return json(['code' => 0, 'msg' => '添加成功']);
            } catch (\Exception $e) {
                return json(['code' => 1, 'msg' => '添加失败：' . $e->getMessage()]);
            }
        }
        
        return $this->view('member/level/add');
    }
    
    public function edit(Request $request)
    {
        if ($request->method() === 'POST') {
            $data = $request->post();
            
            try {
                // 验证等级名称是否重复
                $exists = Db::table('ky_member_level')
                    ->where('name', $data['name'])
                    ->where('id', '<>', $data['id'])
                    ->exists();
                    
                if ($exists) {
                    throw new \Exception('等级名称已存在');
                }
                
                // 更新等级
                Db::table('ky_member_level')
                    ->where('id', $data['id'])
                    ->update([
                        'name' => $data['name'],
                        'discount' => $data['discount'],
                        'points_rate' => $data['points_rate'],
                        'upgrade_points' => $data['upgrade_points'],
                        'status' => $data['status'],
                        'remark' => $data['remark'],
                        'updated_at' => date('Y-m-d H:i:s')
                    ]);
                
                return json(['code' => 0, 'msg' => '编辑成功']);
            } catch (\Exception $e) {
                return json(['code' => 1, 'msg' => '编辑失败：' . $e->getMessage()]);
            }
        }
        
        $id = $request->get('id');
        $level = Db::table('ky_member_level')->where('id', $id)->first();
        
        return $this->view('member/level/edit', ['level' => $level]);
    }
    
    public function delete(Request $request)
    {
        $id = $request->post('id');
        
        try {
            // 检查是否有会员使用该等级
            $hasMember = Db::table('ky_member')
                ->where('level', $id)
                ->exists();
                
            if ($hasMember) {
                throw new \Exception('该等级下有会员，不能删除');
            }
            
            // 删除等级
            Db::table('ky_member_level')->where('id', $id)->delete();
            
            return json(['code' => 0, 'msg' => '删除成功']);
        } catch (\Exception $e) {
            return json(['code' => 1, 'msg' => '删除失败：' . $e->getMessage()]);
        }
    }
} 